package com.pennywise.controller;

import com.pennywise.common.response.Result;
import com.pennywise.model.CategoryStats;
import com.pennywise.model.MonthlyStats;
import com.pennywise.service.TransactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "统计分析")
@RestController
@RequestMapping("/api/statistics")
@RequiredArgsConstructor
public class StatisticsController {

    private final TransactionService transactionService;

    @Operation(summary = "获取分类统计")
    @GetMapping("/category")
    public Result<List<CategoryStats>> getCategoryStats(
            @Parameter(description = "交易类型（收入/支出）", required = true) 
            @RequestParam String type,
            
            @Parameter(description = "开始时间") 
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            
            @Parameter(description = "结束时间") 
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        
        // 验证交易类型
        if (!type.equals("收入") && !type.equals("支出")) {
            return Result.error(400, "交易类型必须是'收入'或'支出'");
        }
        
        // 设置默认时间范围
        if (startTime == null) {
            startTime = LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        }
        if (endTime == null) {
            endTime = LocalDateTime.now();
        }
        
        // 验证时间范围
        if (startTime.isAfter(endTime)) {
            return Result.error(400, "开始时间不能晚于结束时间");
        }
        
        return Result.success(transactionService.getCategoryStats(type, startTime, endTime));
    }
    
    @Operation(summary = "获取本月分类统计")
    @GetMapping("/category/current-month")
    public Result<List<CategoryStats>> getCurrentMonthCategoryStats(
            @Parameter(description = "交易类型（收入/支出）", required = true) 
            @RequestParam String type) {
        
        // 验证交易类型
        if (!type.equals("收入") && !type.equals("支出")) {
            return Result.error(400, "交易类型必须是'收入'或'支出'");
        }
        
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startOfMonth = now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        
        return Result.success(transactionService.getCategoryStats(type, startOfMonth, now));
    }
    
    @Operation(summary = "获取月度统计")
    @GetMapping("/monthly")
    public Result<List<MonthlyStats>> getMonthlyStats(
            @Parameter(description = "开始时间") 
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            
            @Parameter(description = "结束时间") 
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        
        if (startTime == null) {
            startTime = LocalDateTime.now().withDayOfYear(1).withHour(0).withMinute(0).withSecond(0);
        }
        if (endTime == null) {
            endTime = LocalDateTime.now();
        }
        
        return Result.success(transactionService.getMonthlyStats(startTime, endTime));
    }
    
    @Operation(summary = "获取当前月度统计")
    @GetMapping("/monthly/current")
    public Result<MonthlyStats> getCurrentMonthStats() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startOfMonth = now.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        
        List<MonthlyStats> stats = transactionService.getMonthlyStats(startOfMonth, now);
        return Result.success(stats.isEmpty() ? new MonthlyStats() : stats.get(0));
    }
} 